<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="../../css/layui.css">
    <script type="text/javascript" src="../../layui.js"></script>
    <script type="text/javascript" src="../../js/jquery-2.2.4.js"></script>
</head>
<body>

<div style="margin-top: 1%;">
    <form class="layui-form" action="">

        <div class="layui-inline ">
            <label class="layui-form-label">房间号:</label>
            <div class="layui-input-inline">
                <input type="text" id="startTimeval" style="width: 220px;" lay-verify="date" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">MAC:地址</label>
            <div class="layui-input-inline">
                <input type="text" id="endTimeval" lay-verify="date"  style="width: 220px;" autocomplete="off" class="layui-input">
            </div>
        </div>


        <div class="layui-inline">
            <button type="button" class="layui-btn" id="Searchbtn">搜索</button>
        </div>
    </form>
</div>

<table class="layui-table" id="lishidata" lay-filter="lishidata"></table>
</body>
</html>

<script type="text/html" id="caozuo">

    <button class="btn layui-btn" lay-event="groupStatusEvent">使用</button>
</script>

<script type="text/javascript">
    layui.use(['form', 'layedit', 'laydate','table','util','layer'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate
            ,table = layui.table
            , layer = layui.layer
            , util = layui.util;

        var s=layui.data("s");
        var s2=layui.data("s2");
        var terminal_station=s.terminal;
        var tid=s2.terminal2.terminal_key_id;

        table.render({
            elem: '#lishidata'
            , id: "t"
            , toolbar: "#tol"
            , url: '/admin/terminal-key/findByStationIdQueryTerminal?terminal_station='+terminal_station
            , cols: [[
                {
                    field: 'terminal_id', title: '终端ID', width: "24%",
                    align: "center"
                }
                ,
                {
                    field: 'terminal_mac', title: 'MAC地址',
                    // width: "25%",
                    align: "center"
                }
                , {
                    field: 'terminal_room',
                    // width: "24%",
                    align: "center", title: '房间号'
                }
                , {
                    field: 'terminal_key',
                    // width: "24%",
                    align: "center", title: '秘钥'
                }
                ,{
                    toolbar:'#caozuo',
                    align:"center",
                    title:"操作",
                    fixed:"right"
                }
            ]],
            page: true
        });

        table.on('tool(lishidata)',function (obj) {
            var d = new Date();
            var year=  d.getFullYear();

            var month=  d.getMonth()+1;
            var startDate=  d.getDate();
            var endDate=  d.getDate()+1;
            var hours= d.getHours();
            var minutes= d.getMinutes();
            var seconds= d.getSeconds();
            var startTime= year+"-"+month+"-"+startDate+" "+hours+":"+minutes+":"+seconds;
            var endTime= year+"-"+month+"-"+endDate+" "+hours+":"+minutes+":"+seconds;


            var getdata=obj.data; //获取当前行数据
            var layEvent=obj.event; //获取lay-event对应的值,(也可以是表头的event参数对应的值)
            var tr=obj.tr; //获取当前行tr的DOM对象(如果有的话)

            switch(layEvent){

                case "groupStatusEvent":
//询问框

                    layer.confirm('你确定要给该终端设置密钥吗？', {
                            btn: ['标准型','钟点型','取消'] //按钮
                        }, function(){

                            $.ajax({
                                url:"/admin/terminal-key/usePasswordKeyRoomType",
                                type:"get",
                                data:{
                                    terminal_key_id:s2.terminal2.terminal_key_id,
                                    terminal_id:getdata.terminal_id,
                                    terminal_key_value:s2.terminal2.terminal_key_value,
                                    terminal_key_room:getdata.terminal_room,
                                    terminal_key_room_type:"标准房",
                                    terminal_key_begin_time:startTime,
                                    terminal_key_end_time:endTime
                                }
                            })
                            layer.msg('设置成功！', {icon: 1,time: 1000
                                ,
                                end:function () {
                                    localStorage.setItem("ifclone","1");
                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    //layer.msg(index);
                                    parent.layer.close(index); //再执行关闭
                                    parent.location.reload();
                                }
                            });
                            //     layer.msg(dateTime, {icon: 1});

                        }, function(){
                            layer.open({
                                type: 1,
                                title: "请设置到期时间",
                                closeBtn: 0,
                                shadeClose: true,
                                area: ['500px', '350px'],
                                skin: 'yourclass',
                                content: '<div class="layui-fluid">\n' +
                                    '  <div class="layui-form-item ">\n' +
                                    '    <div class="layui-inline ">\n' +
                                    '      <label class="layui-form-label" >到期时间:</label>\n' +
                                    '      <div class="layui-input-inline">\n' +
                                    '        <input type="text" id="end" lay-verify="date" style="width: 250px" autocomplete="off" class="layui-input">\n' +
                                    '      </div>\n' +
                                    '    </div>\n' +
                                    '  </div>\n' +
                                    '\n' +
                                    '  <div>\n' +
                                    '    <span style="color: red;margin-left: 100px">*右击左下角选择时间可选择时分秒</span>\n' +
                                    '  </div>\n' +
                                    '  <br>\n' +
                                    '  <div class="layui-inline">\n' +
                                    '    <button type="button" class="layui-btn" style="margin-left: 250px" id="queding">确认</button>\n' +
                                    '  </div>\n' +
                                    '\n' +
                                    '</div>',
                                success: function(){
                                    laydate.render({

                                        elem: "#end",
                                        type: 'datetime',
                                        trigger: 'click',
                                        done: function(value, date, endDate) {

                                            var endtime = new Date(value).getTime();
                                            if(endtime < startTime) {

                                                alert("no")
                                            }
                                        }

                                    });
                                    $(document).on('click', '#queding', function() {
                                        var endtime =$("#end").val();
                                        if (endtime<startTime){
                                            layer.msg('结束时间不能小于当前时间'),{icon: 1,time: 1000};
                                            return;
                                        }
                                        $.ajax({
                                            url:"/admin/terminal-key/usePasswordKeyRoomType",
                                            type:"get",
                                            data:{
                                                terminal_key_id:s2.terminal2.terminal_key_id,
                                                terminal_id:getdata.terminal_id,
                                                terminal_key_value:s2.terminal2.terminal_key_value,
                                                terminal_key_room:getdata.terminal_room,
                                                terminal_key_room_type:"钟点房",
                                                terminal_key_begin_time:startTime,
                                                terminal_key_end_time:endTime
                                            }
                                        })
                                        layer.msg('设置成功！', {icon: 1,time: 1000
                                            ,
                                            end:function () {
                                                localStorage.setItem("ifclone","1");
                                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                                //layer.msg(index);
                                                parent.layer.close(index); //再执行关闭
                                                parent.location.reload();
                                            }
                                        });


                                    });
                                },
                                error : function() {
                                    alert("操作失败！");
                                }

                            });
                        },function () {
                            layer.close();
                        }

                    );
                    break;


            }

        });

        $("#Searchbtn").click(function(){
            table.reload("t",{
                where:{
                    startTime:$("#startTimeval").val(),
                    endTime:$("#endTimeval").val()
                },
                page:{
                    curr:1
                }
            });
        });


        //日期
        laydate.render({
            elem: '#startTimeval'
            ,type: 'datetime'
        });
        laydate.render({
            elem: '#endTimeval'
            ,type: 'datetime'
        });


    });


</script>

<!--table日期转换格式-->
<script>
    function Format(datetime,fmt) {
        if (parseInt(datetime)==datetime) {
            if (datetime.length==10) {
                datetime=parseInt(datetime)*1000;
            } else if(datetime.length==13) {
                datetime=parseInt(datetime);
            }
        }
        datetime=new Date(datetime);
        var o = {
            "M+" : datetime.getMonth()+1,                 //月份
            "d+" : datetime.getDate(),                    //日
            "h+" : datetime.getHours(),                   //小时
            "m+" : datetime.getMinutes(),                 //分
            "s+" : datetime.getSeconds(),                 //秒
            "q+" : Math.floor((datetime.getMonth()+3)/3), //季度
            "S"  : datetime.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt))
            fmt=fmt.replace(RegExp.$1, (datetime.getFullYear()+"").substr(4 - RegExp.$1.length));
        for(var k in o)
            if(new RegExp("("+ k +")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        return fmt;
    }
</script>


